<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * App\Models\UserAmount
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property string $pre_money 变更前余额
 * @property string $money 变更金额
 * @property string $after_money 变更后余额
 * @property string|null $remark 变更备注
 * @property int $type 变更类型:0=后台修改,1=订单金额,2=提现金额
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount query()
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount whereAfterMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount wherePreMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserAmount whereUserId($value)
 * @mixin \Eloquent
 */
class UserAmount extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'user_amount';

    protected $fillable = [
        'user_id','type','pre_money','money','after_money','remark'
    ];



    /**
     * 获取用户的余额记录
     * @param $user_id
     * @return array
     */
    public static function getList($user_id): array
    {
        $type = request()->input('type',0);
        $pageSize = request()->input('pageSize',10);
        $list = self::whereUserId($user_id)
            ->when($type>0,function ($query) use ($type) {
                $query->where("type",$type);
            })
            ->orderByDesc("id")
            ->paginate($pageSize);
        $data['items'] = $list->items();
        $data['lastPage'] = $list->lastPage();
        $data['total'] = $list->total();
        return $data;
    }

}
